package com.mall.shopping.services;

import com.mall.shopping.ShoppingRecommendService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.dto.RecommendResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @description: TODO
 * @author: jiaolei
 * @date: 2021/12/13 10:01
 */
@Service
@Transactional
public class ShoppingRecommendServiceImpl implements ShoppingRecommendService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ContentConverter contentConverter;
    @Override
    public RecommendResponse recommend() {
        RecommendResponse recommendResponse=new RecommendResponse();

        //将panel表的数据以列表形式返回
        try {
            List<Panel> panels = panelMapper.selectAll();
            for (Panel panel : panels) {
                Example example = new Example(PanelContent.class);
                example.createCriteria().andEqualTo("panelId",panel.getId());
                //新建一个PanelContentItem列表，经过两次查询将数据放入该列表
                ArrayList<PanelContentItem> panelContentItems = new ArrayList<>();
                List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
                for (PanelContent panelContent : panelContents) {
                    //该panel没有商品，continue
                    if(panelContent.getProductId()==null) continue;
                    Item item = itemMapper.selectByPrimaryKey(panelContent.getProductId());
                    //item有可能是null
                    if(item==null){

                    }
                    //将两次查询的数据放入panelContentItem
                    PanelContentItem panelContentItem = contentConverter.panelContent2PanelContentItem(panelContent);
                    panelContentItem.setProductName(item.getTitle());
                    panelContentItem.setSubTitle(item.getSellPoint());
                    panelContentItem.setSalePrice(item.getPrice());
                    //将panelContentItem装入列表
                    panelContentItems.add(panelContentItem);
                }
                //将panelContentItems装入panel
                panel.setPanelContentItems(panelContentItems);

            }
            //将panels转换成panelDtos
            List<PanelDto> panelDtos = contentConverter.panels2Dtos(panels);
            HashSet<PanelDto> panelDtos1 = new HashSet<>(panelDtos);
            //将panelDtos装入recommendResponse
            recommendResponse.setPanelContentItemDtos(panelDtos1);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            recommendResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return recommendResponse;
    }
}
